var i;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var ResUtil = require("AA_ResUtil");
var _decorator = cc._decorator;
var _ccclass = _decorator.ccclass;
var _property = _decorator.property;
var _menu = _decorator.menu;
var h = _decorator.requireComponent;
var _def_LayoutExtent = function (e) {
  function _creator() {
    var t = null !== e && e.apply(this, arguments) || this;
    t.itemEx = null;
    t.itemPrefabEx = null;
    t.scale = 1;
    t._itemTmp = null;
    t._isInit = false;
    t._listItems = new Map();
    t._addId = 0;
    t._excuting = new Map();
    return t;
  }
  __extends(_creator, e);
  _creator.prototype.start = function () {
    this._init();
  };
  _creator.prototype._init = function () {
    if (!this._isInit) {
      if (this.itemEx) {
        this._itemTmp = ResUtil.ResUtil.instantiate(this.itemEx);
        this.scale = this.itemEx.scale;
        this.itemEx.active = false;
      } else {
        this.itemPrefabEx && (this._itemTmp = ResUtil.ResUtil.instantiate(this.itemPrefabEx));
      }
      this._isInit = true;
    }
  };
  _creator.prototype.setItemRenderer = function (e, t) {
    this._itemRender = e;
    this._target = t;
  };
  _creator.prototype.setNumItems = function (e, t, o, n, i) {
    var a = this;
    undefined === t && (t = 0);
    undefined === i && (i = true);
    ResUtil.ResUtil.removeAllChildren(this.node, i);
    this._listItems.clear();
    this._init();
    var r = [];
    var c = e.length;
    if (t <= 0) {
      for (var u = 0; u < c; u++) {
        var l = this.itemGenerator(u, e[u]);
        r.push(l);
      }
      this._isInit = true;
      o && o.call(n);
    } else {
      this.stopLastExecutePerFrame();
      this._executePerFrame(this._getItemGenerator(e, c), t, function () {
        a._isInit = true;
        o && o.call(n);
      }, this, r);
    }
    return r;
  };
  _creator.prototype.getItems = function () {
    return this._listItems;
  };
  _creator.prototype._getItemGenerator = function (e, t) {
    var o;
    return __generator(this, function (n) {
      switch (n.label) {
        case 0:
          t = t || e.length;
          o = 0;
          n.label = 1;
        case 1:
          if (o < t) {
            return [4, this.itemGenerator(o, e[o])];
          } else {
            return [3, 4];
          }
        case 2:
          n.sent();
          n.label = 3;
        case 3:
          o++;
          return [3, 1];
        case 4:
          return [2];
      }
    });
  };
  _creator.prototype.itemGenerator = function (e, t) {
    var o = this._initItem(e);
    this._itemRender && this._itemRender.call(this._target, o, t, e);
    return o;
  };
  _creator.prototype._initItem = function (e) {
    var t = ResUtil.ResUtil.instantiate(this._itemTmp);
    t.name = e + "";
    t.active = true;
    t.parent = this.node;
    t.scale = this.scale;
    this._listItems.set(e, t);
    return t;
  };
  _creator.prototype._executePerFrame = function (e, t, o, n, i) {
    var a = e;
    var r = this._addId;
    var c = this;
    this._excuting.set(r, true);
    this._addId++;
    (function e() {
      c._excuting.get(r) || (a = null);
      if (a) {
        var s = new Date().getTime();
        for (var u = a.next();; u = a.next()) {
          i && u.value && i.push(u.value);
          if (null == u || u.done) {
            o && o.call(n, i);
            return void c._excuting.delete(r);
          }
          if (new Date().getTime() - s > t) {
            return void c.scheduleOnce(function () {
              e();
            });
          }
        }
      }
    })();
  };
  _creator.prototype.stopLastExecutePerFrame = function () {
    var e = this._addId - 1;
    this.stopExecutePerFrame(e);
  };
  _creator.prototype.stopExecutePerFrame = function (e) {
    this._excuting.has(e) && this._excuting.set(e, false);
  };
  __decorate([_property(cc.Node)], _creator.prototype, "itemEx", undefined);
  __decorate([_property(cc.Prefab)], _creator.prototype, "itemPrefabEx", undefined);
  __decorate([_property({
    type: cc.Float
  })], _creator.prototype, "scale", undefined);
  return __decorate([_ccclass, _menu("UI组件/Layout Extent"), h(cc.Layout)], _creator);
}(cc.Component);
exports.default = _def_LayoutExtent;